<template>
 	<Layout class="p-5 bg-white">
 		<Form calss="p-5" :model="cardType" :label-width="150">
			<Row>
				<Col span="8">
					<FormItem class="w-100" label="会员卡名称">
						<Input v-model="cardType.name" placeholder="请输入会员卡名称"></Input>
					</FormItem>
					<FormItem class="w-100" label="有效时长/天">
						<Input @keyup.native="changeTimeLongs" v-model="cardType.timeLongs" placeholder="请输入时长"></Input>
					</FormItem>
					<FormItem class="w-100" label="价格">
						<Input @keyup.native="changeAmount" v-model="cardType.amount" placeholder="请输入价格"></Input>
					</FormItem>
					<FormItem class="w-100"  label="会员卡状态">
						<Select :class="'w-100'" v-model="cardType.statu" placeholder="请选择会员卡状态" style="width:200px" @change="" clearable>
							<Option v-for="item in status" :value="item.value" :key="item.value">{{ item.label }}</Option>
						</Select>
					</FormItem>
					<FormItem class="text-right" > 
						<Button @click="submit" type="primary">提交</Button>
						<Button @click="cancel" class="ml-2">取消</Button>
					</FormItem>
				</Col>
			</Row>
 		</Form>
 	</Layout>
 </template>
 
 <script>
 	export default {
 		data() {
 			return {
					cardType:{
						cardTypeId:"0",
						name:"",
						timeLongs:"",
						amount:"",
						statu:"",
					},
  					status:[{
  						value: "1",
  						label:"可用"
  					},
  					{
  						value: "0",
  						label:"禁用"
  					}],
					
  			};
 		},
 		
 		methods: {
			changeTimeLongs() {      
				let str = '' + this.cardType.timeLongs;
				if (str.indexOf('.') != -1) {        
					let arr = str.split('');
			        arr.splice(arr.length - 1);        
					let str2 = arr.join('');        
					this.cardType.timeLongs = +str2;
			      }
				},
			changeAmount() {
				let str = '' + this.cardType.amount;
				if (str.indexOf('.') != -1) {        
					let arr = str.split('');
				    arr.splice(arr.length - 1);        
					let str2 = arr.join('');        
					this.cardType.amount = +str2;
				    }
			    },
			function() {
				this.search();
			},
			search: function() {
				this.http.get({
					url: "/client/card-type/info",
					param: {
						statu: this.$data.records.statu
					}
				}).then(data => {
					this.$data.tableInfo.data = data.records;
				});
			},
 			submit: function() {
				if(this.cardType.name != null && this.cardType.name != ""){
					if(this.cardType.timeLongs != null && this.cardType.timeLongs != "" && this.cardType.timeLongs > 0){
						if(this.cardType.amount != null && this.cardType.amount != "" && this.cardType.amount > 0){
							let self = this;
							this.http.post({
								url: "/client/card-type/info",
								param: {
									cardType:this.$data.cardType,
									price:this.$data.cardType.amount * 10 *10,
								}
							}).then(data => {
								self.message.info("新增成功");
								this.$tabs.close();//关闭窗口
								this.$tabs.open("/cardType")//页面跳转
							});
						}else{
							this.message.error("价格不能为空且是数字");
						}
					}else{
						this.message.error("时长不能为空且是数字");
					}
				}else{
					this.message.error("会员卡名称必须输入");
				}
 			},
 			cancel: function() {
				this.$router.go(-1);
 				this.$tabs.close();
 			},
			created: function() {
				this.init();
			},
 		}
 	}
 </script>
 
 <style>
 </style>